<template>
<div class="main">
    <el-form width="500px" :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
  <el-form-item label="角色名称" prop="name">
    <el-input 
    v-model="formData.name" 
    maxlength="20" 
    minlength="3"
    show-word-limit
    width="300px"
    placeholder="请输入角色名"
    ></el-input>
  </el-form-item>


    <el-form-item label="角色描述" prop="description">
    <el-input 
    v-model="formData.description" 
    maxlength="20" 
    minlength="3"
    show-word-limit
    width="300px"
    placeholder="请输入描述"
    ></el-input>
  </el-form-item>
  

  <el-form-item label="权限" >
    <el-select v-model="formData.id" placeholder="请选择权限">
      <el-option :label="category.name" :value="category.id" v-for="category in categories" :key="category.id">{{category.name}}</el-option>
    </el-select>
  </el-form-item>

  <el-form-item>
    <el-button type="primary" @click="add">立即创建</el-button>
    <el-button @click="resetForm('ruleForm')">重置</el-button>
  </el-form-item>
</el-form>
</div>
  
</template>

<script>
    import global from "@/global.js";
    export default {
        created() {
            /**
             *获取权限分类
            * 
            */
            this.$axios.post(global.server + '/api/perm/list')
                .then(response => {
                    console.log(response);
                    let result = response.data;
                    if (result.code == 200) {
                        this.categories = result.data;
                    }
                });
        },
    data() {
      return {
        categories: "",
        companies: "",
        customers: "",
        emps: "",

        formData: {
            name: "",
            categoryId: '',
            valuation: "",
            companyId: '',
            cusId: '',
            empId: '',
            image: ""
        },

        ruleForm: {
          name: '',
        },
        rules: {
          name: [
            
            { min: 3, max: 5, message: '长度在 3 到 20 个字符', trigger: 'blur' }
          ],

        }
      
      };
    },
    methods: {
        add(){
            console.log(this.formData);
            this.$alert("添加成功!");
             this.$axios.post(global.server + '/api/role/add',
                    this.formData)
                    .then(response => {
                        console.log("role-----------------------------|")
                        console.log(response);
                        let result = response.data;
                        if (result.code == 200) {
                            this.emps = result.data;
                            this.$alert("添加成功!");
                        }
                    });
        },
      
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<style scope>
    .main{
      width: 319px;
    }
</style>